﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using Benefit.Models.Sys;
namespace Benefit.Models.Month
{
    /// <summary>
    /// 账户资金变动(对于个人多账号后显示的总计值在while循环后重新写一个对象写入)这个是显示在第一行的总数(u)
    /// </summary>
    public class AccountMonthChange
    {
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public int AccountId { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public virtual Account Account { get; set; }
        /// <summary>
        /// 交易年
        /// </summary>
        public int Year { get; set; }
        /// <summary>
        /// 交易月
        /// </summary>
        public int Month { get; set; }

        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }



        ///// <summary>
        ///// 删除当月所有记录
        ///// </summary>
        ///// <param name="tradeHistoryId"></param>
        //public void Delete(int month, int year, DB.DBManager db)
        //{
        //    var query = from t in db.AccountMonthChange where t.Month == month && t.Year == year select t;
        //    foreach (Data.Month.AccountMonthChange c in query)
        //    {
        //        db.AccountMonthChange.Remove(c);
        //    }
        //    db.SaveChanges();
        //}

        ///// <summary>
        ///// 保存账号月变化情况
        ///// </summary>
        ///// <param name="tradeHistoryId">插入时间编号</param>
        ///// <param name="pdate">插入时间</param>
        //public void InitMonthAccountChange(int month, int year, DB.DBManager db)
        //{
        //    var query = db.Accounts.ToList();
        //    foreach (Sys.Account account in query)
        //    {
        //        AccountMonthChange change = new AccountMonthChange();
        //        change.AccountId = account.Id;
        //        var _query = from t in db.AccountDayChange where t.TradeHistory.MPdate == month && t.TradeHistory.YPdate == year && t.AccountId == account.Id select t;
        //        var query2 = from t in db.AccountMonthChange where t.Month == month && t.Year == year && t.AccountId == account.Id select t;
        //        if (query2.Count() > 0)
        //        {
        //            change = query2.First();
        //        }
        //        if (_query.Count() > 0)
        //        {
        //            change.BillCount = _query.Sum(a => a.BillCount);
        //            change.DayCount = _query.Sum(a => a.DayCount);
        //            change.Free = _query.Sum(a => a.Free);
        //            change.LoseCount = _query.Sum(a => a.LoseCount);
        //            change.Profit = _query.Sum(a => a.Profit);
        //            change.SumLost = _query.Sum(a => a.SumLost);
        //            change.SumWin = _query.Sum(a => a.SumWin);
        //            change.UsedMargin = _query.Sum(a => a.UsedMargin);
        //            change.WinCount = _query.Sum(a => a.WinCount);

        //        }
        //        change.Year = year;
        //        change.Month = month;
        //        if (query2.Count() == 0)
        //        {
        //            db.AccountMonthChange.Add(change);
        //        }

        //    }
        //    db.SaveChanges();
        //}
    }
}